package com.autologistics.action;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.autologistics.model.Disparter;
import com.autologistics.service.DisparterService;
import com.autologistics.util.GobalValue;
import com.autologistics.util.PageBean;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class DispartAction extends ActionSupport implements ModelDriven<Disparter>{
	DisparterService disparterService;
	Disparter disparter = new Disparter();
	List<Disparter> listdisparter;
	private int renId;
	private int renrole;
	private String renname;
	private String message;
	private PageBean pageBean;
	//默认是1
	private int page=1;
	
	
	
	
	
	/**
	 * @author lsyhyp@126.com
	 * 列出所有的配送员
	 * @return
	 */
	public String listAllDisparter(){
		if (!checkLogin()){
			return "loginfail";
		}
		if (renrole!=0 && renrole!=4){
			return "wrongrole";
		}
		listdisparter=disparterService.listAll();
		if (listdisparter==null){
			message="暂无配送员";
			return SUCCESS;
		}
		pageBean = disparterService.queryForOnePage(GobalValue.pageSize, page, listdisparter.size());
		listdisparter=pageBean.getDisparterList();
		return SUCCESS;
	}
	
	
	/**
	 * 准备添加配送员
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String preAddDisparter(){
		if (!checkLogin()){
			return "loginfail";
		}
		if (renrole!=0 && renrole!=4){
			return "wrongrole";
		}
		return SUCCESS;
	}
	
	
	/**
	 * 添加配送员
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String addDisparter(){
		if (!checkLogin()){
			return "loginfail";
		}
		if (renrole!=0 && renrole!=4){
			return "wrongrole";
		}
		Timestamp ts = new Timestamp(new Date().getTime());
		disparter.setJoinTime(ts);
		disparter.setState(1);
		disparterService.add(disparter);
		return this.listAllDisparter();
	}
	
	
	/**
	 * 准备编辑配送员
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String preEditDisparter(){
		if (!checkLogin()){
			return "loginfail";
		}
		if (renrole!=0 && renrole!=4){
			return "wrongrole";
		}
		disparter=disparterService.getById(disparter.getId());
		return SUCCESS;
	}
	/**
	 * 编辑配送员
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String editDisparter(){
		disparterService.update(disparter);
		return this.listAllDisparter();
	}
	
	public boolean checkLogin(){
		
		Map<String, Object> attibutes = ActionContext.getContext().getSession();
		if (attibutes.get("id")==null){
			message="未登陆，请重新登陆";
			return false;
		}
		String s=attibutes.get("id").toString();
		renId=Integer.parseInt(s);
		if (attibutes.get("role")!=null){
			s=attibutes.get("role").toString();
			renrole=Integer.parseInt(s);
		}
		renname=attibutes.get("name").toString();
		return true;
	}
	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRenId() {
		return renId;
	}

	public void setRenId(int renId) {
		this.renId = renId;
	}

	public int getRenrole() {
		return renrole;
	}

	public void setRenrole(int renrole) {
		this.renrole = renrole;
	}

	public String getRenname() {
		return renname;
	}

	public void setRenname(String renname) {
		this.renname = renname;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Disparter getDisparter() {
		return disparter;
	}

	public void setDisparter(Disparter disparter) {
		this.disparter = disparter;
	}

	public List<Disparter> getListdisparter() {
		return listdisparter;
	}

	public void setListdisparter(List<Disparter> listdisparter) {
		this.listdisparter = listdisparter;
	}

	public DisparterService getDisparterService() {
		return disparterService;
	}

	public void setDisparterService(DisparterService disparterService) {
		this.disparterService = disparterService;
	}
	@Override
	public Disparter getModel() {
		// TODO Auto-generated method stub
		return this.disparter;
	}
}
